package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.my.bean.ResultPage;
import com.my.bean.Singular;
import com.my.bean.VoPage;
import com.my.service.SingularService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 单数 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2018-09-01
 */
@Controller
@RequestMapping("/singular")
public class SingularController {

    @Autowired
    SingularService singularService;
    //创建日志对象
    Logger logger = Logger.getLogger(SingularController.class);

    /**
     * 返回总页面
     */
   /* @RequestMapping("/singular")
    public String singular(){
        return "singular";
    }*/


    /**
     * 返回销售管理的销售统计页面
     */
    @RequestMapping("/monthlySales")
    public String monthlySales(){
        return "monthlySales";
    }


    /**
     * 添加单数
     * @param singular
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public boolean insert(Singular singular){
        boolean flag = singularService.insert(singular);
        String msg = flag?"添加成功"+singular:"添加失败";
        logger.info(msg);
        return false;
        /*if(singularService.insert(singular)){
            logger.info("{insert|添加成功}");
            return true;
        }else{
            logger.info("{insert|添加失败}");
            return false;
        }*/
    }

    /**
     * 查询所有
     * @param voPage
     * @return
     */
    @RequestMapping("/select")
    @ResponseBody
    public ResultPage<Singular> select(VoPage voPage,Singular singular){
       //Singular singular= JSONObject.parseObject(param,Singular.class);
        ResultPage<Singular> list = singularService.selectMyPage(voPage,singular);
        return list;
    }

    /**
    * 删除单数
    */
    @RequestMapping("/deleteSingular")
    @ResponseBody
    public boolean deleteSingular(Long id){
        return singularService.deleteById(id);
    }

    /**
     * 修改单数
     * @param singular
     * @return
     */
    @RequestMapping("/updateSingular")
    @ResponseBody
    public String updateSingular(Singular singular){
        List<Singular> list = new ArrayList<>();
        list.add(singular);
        if(singularService.updateBatchById(list)){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }
    @RequestMapping("/singular_add")
    public String singluarAdd(){
        return "/Alert/singular_add";

    }
    @RequestMapping("/singular_edit")
    public String singularEdit(){
        return "/Alert/singular_edit";

    }



    @RequestMapping("/monthly_add")
    public String monthlyAdd(){
        return "/Alert/monthly_add";
    }

	
}
